package com.example.spring_transaction.controller;

import com.example.spring_transaction.service.LogService;
import com.example.spring_transaction.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @className: TestController2
 * @author: 芃
 * @description: 声明式事务
 * @date: 2024/12/30 18:08
 */
@RestController
@RequestMapping("/test2")
@Slf4j
public class TestController2 {
    @Autowired
    private UserService userService;


    @Autowired
    private LogService logService;

    @RequestMapping("/registry")
    @Transactional // ->自动进行事务的提交和回滚
    //所在的方法没有抛出异常时，事务就提交，抛出异常时，事务就回滚
    //报错了，并且没捕获 or 报错了，捕获了，但是没有处理，重新抛出了
    public String registry(String userName,String password){
        Integer result =  userService.addUser(userName,password);
        log.info("User added successfully");
        return "注册成功";
    }



    //抛出异常，程序报错，没有处理 ->自动回滚
    @RequestMapping("/registry2")
    @Transactional
    public String registry2(String userName,String password){
        Integer result =  userService.addUser(userName,password);
        log.info("User added successfully");
        int a = 10 / 0;
        return "注册成功";

    }

    //抛出异常，捕获异常 ->不回滚，自动提交
    @RequestMapping("/registry3")
    @Transactional
    public String registry3(String userName,String password){
        Integer result =  userService.addUser(userName,password);
        log.info("User added successfully");
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.info("程序执行错误");
        }
        return "注册成功";
    }

    //抛出异常，捕获异常，又重新抛出 ->自动回滚
    @RequestMapping("/registry4")
    @Transactional
    public String registry4(String userName,String password){
        Integer result =  userService.addUser(userName,password);
        log.info("User added successfully");
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.info("程序执行错误");
            throw e;
        }
        return "注册成功";
    }

    //抛出异常，捕获到异常，不处理，但是手动回滚事务
    @RequestMapping("/registry5")
    @Transactional
    public String registry5(String userName,String password){
        Integer result =  userService.addUser(userName,password);
        log.info("User added successfully");
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.info("程序执行错误");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//当前事务回滚
        }
        return "注册成功";
    }

    @RequestMapping("/registry6")
    @Transactional//设置：遇到IOException时，事务也回滚
    public String registry6(String userName,String password) throws IOException {
        Integer result =  userService.addUser(userName,password);
        log.info("User added successfully");
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.info("程序执行错误");
            throw new RuntimeException("其他异常");
            //如果是IOException 或者error时，事务就会提交
            // 如果是其他运行时异常，事务就会回滚
        }
        return "注册成功";
    }

    @RequestMapping("/registry7")
    @Transactional(rollbackFor = IOException.class)//设置：遇到IOException时，事务也回滚
    public String registry7(String userName,String password) throws IOException {
        Integer result =  userService.addUser(userName,password);
        log.info("User added successfully");
        try {
            int a = 10 / 0;
        }catch (Exception e){
            log.info("程序执行错误");
            throw new IOException("IOException");
        }
        return "注册成功";
    }

    //总结：只要有异常抛出，事务就会自动回滚


}
